package com.erp.database.filter;


import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by wuxf on 14-2-25.
 */
public class LoginFilter implements Filter {
    private static Logger logger = LoggerFactory.getLogger(LoginFilter.class);

//    private UserDao userDao = null;
//    private UserUtil userUtil = null;

    private String[] excludePatterns = null;
    private String[] excludeTypes = null;
    private String loginPage = null;

    /**
     * 取出url里文件名的那一段
     */
    private static String url_reg_ex="(((.*/)(.*)\\?)(.*))|((.*/)(.*))";

    private static Pattern pattern = Pattern.compile(url_reg_ex);


//    public LoginFilter(UserDao userDao, UserUtil userUtil){
//        this.userDao = userDao;
//        this.userUtil = userUtil;
//    }

    private static String getPath(String url) {
        String path = "";
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            path = matcher.group(4);
            if (path == null) {
                path = matcher.group(8);
            }
        }
        if (path == null) {
            path = "";
        }
        return path;
    }

    /**
     * 检查是否是不需要验证的url
     * @return
     */
    private boolean inExcludePatterns(String url) {
        boolean match = false;
        String path = getPath(url);
        if (this.excludePatterns != null && this.excludePatterns.length > 0) {
            for(String excludeUrl : this.excludePatterns) {
                if (excludeUrl.equals(path)) {
                    match = true;
                    break;
                }
            }
        }
        if (!match) {
            String type = null;
            if (path.contains(".")) {
                String[] parts = path.split("\\.");
                type = parts[parts.length - 1];
            }
            if (this.excludeTypes != null && this.excludeTypes.length > 0 && type != null) {
                for (String excludeType : this.excludeTypes) {
                    if (type.equals(excludeType)) {
                        match = true;
                        break;
                    }
                }
            }
        }
        return match;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String excludePatternStr = filterConfig.getInitParameter("excludePatterns");
        if (excludePatternStr != null) {
            this.excludePatterns = excludePatternStr.split(",");
        }
        String excludeTypesStr = filterConfig.getInitParameter("excludeType");
        if (excludeTypesStr != null) {
            this.excludeTypes = excludeTypesStr.split(",");
        }
        loginPage = filterConfig.getInitParameter("loginPage");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String url = request.getRequestURL().toString();
        logger.info(url);
        if (this.inExcludePatterns(url)||url.contains("jd")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
        }
    }

    @Override
    public void destroy() {

    }
}
